WEBVTT

00:01.130 --> 00:06.150
Welcome to the solution for coding exercise 15 and we import pen as an umpire.

00:06.180 --> 00:07.520
Matt plop plop.

00:08.000 --> 00:14.750
And then we import our six stocks and we should select to the appropriate price data to calculate total

00:14.750 --> 00:15.830
returns.

00:16.060 --> 00:25.850
And therefore we selected the adjusted close column because uh the adjusted clause includes dividends.

00:25.880 --> 00:31.970
And then we select the five year period from two 14 to 18 and be over right stocks

00:35.110 --> 00:42.280
so we we have a five year period for our six stocks and later we will need to calculate annualized risk

00:42.310 --> 00:45.430
and return and to make life easier.

00:45.460 --> 00:49.900
We can define you a user defined function annualized the risk and return.

00:50.050 --> 00:56.680
And we actually pass a data frame with daily returns uh to the user defined function and the function

00:56.680 --> 01:03.090
actually calculates uh the mean return and the standard deviation of returns and we can do this here

01:03.100 --> 01:08.410
with the ACT method and uh then the risk and return are getting annualized.

01:08.530 --> 01:12.030
So that's the user defined function.

01:12.850 --> 01:18.340
And first of all we have to create the returns data frame and we can simply do the service of the percentage

01:18.340 --> 01:23.030
change method on the prices on the stock prices.

01:23.110 --> 01:26.920
So that's the return data frame with daily returns.

01:26.920 --> 01:34.320
And then we can pass the return data frame to our user defined function and we create the summary data

01:34.330 --> 01:38.860
frame with annualized the risk and return for our six stocks.

01:38.860 --> 01:44.020
And then we can also visualize the risk and return in a total risk return framework.

01:44.020 --> 01:45.850
So we simply running the cell

01:48.370 --> 01:55.390
and we can see that the maximally the impact on McDonald's stock showed a quite good performance here

01:55.390 --> 01:56.740
on the upper left corner.

01:57.190 --> 02:00.310
And in contrast American Express is pretty weak.

02:00.310 --> 02:05.760
And actually Facebook showed high risk but also high return.

02:05.820 --> 02:11.820
Now the next step we want to create 10000 random portfolios with random weights of the constituents

02:12.540 --> 02:20.190
and uh therefore we define that the number of assets as six and the number of random portfolios 10000

02:22.390 --> 02:28.660
and then we create a number higher rate with 10000 roles and six columns and each element is a random

02:28.660 --> 02:35.860
float between 0 and 1 and we save the matrix and the variable matrix and actually we use the other random

02:35.860 --> 02:36.180
seed.

02:36.190 --> 02:37.410
1 2 3.

02:37.450 --> 02:43.230
And by doing so you should get the very same results as I do here in the solution.

02:43.270 --> 02:50.770
So we you see an umpire dot random dot random and create the 60000 the random floats between 0 and 1

02:51.460 --> 02:56.590
and we reshape those and 2 10000 rows and 6 columns

03:00.490 --> 03:08.020
and then for each row for each random portfolio we can create the weights that sum up to 1 and we can

03:08.020 --> 03:15.940
simply do this by dividing each and every element in a row by the sum of the row and here we have the

03:15.940 --> 03:24.150
weights array and we can also double check if the weights in each row sum up to 1 and this is obviously

03:24.150 --> 03:29.100
the case here so now we have far 10000 random portfolios.

03:29.110 --> 03:32.350
Ten thousand sets of random weights.

03:32.640 --> 03:40.460
And uh we can calculate actually the daily returns of uh these random portfolios by using here the top

03:40.460 --> 03:45.730
method on the returns data frame and pass the transpose the weights matrix.

03:46.040 --> 03:48.970
So this is a matrix multiplication and.

03:48.980 --> 03:52.820
We save the resulting data frame and the rabbit portfolio returns.

03:54.440 --> 03:58.800
So here we have for 10000 random portfolios of the daily returns.

03:58.940 --> 04:03.630
And then we can also annualize the risk and return for the 10000 portfolios.

04:03.710 --> 04:07.690
And we actually save for the resulting data frame and the value portfolio summary.

04:07.700 --> 04:13.020
So we simply pass here the portfolio returns data frame to our user defined function.

04:13.610 --> 04:14.750
And let's have a look here.

04:14.750 --> 04:16.320
So this takes a while

04:19.320 --> 04:24.480
and here we have the last five portfolios with annualized the risk and return.

04:24.480 --> 04:30.920
And then we can also visualize the six constituents and the 10000 the random portfolios here with the

04:30.960 --> 04:35.480
plotting the risk on the x axis and the return on the y axis.

04:35.490 --> 04:37.680
And the same we can do for the constituents

04:40.860 --> 04:48.500
so that's the cloud of random portfolios and to really verify the performance of the random portfolios

04:48.500 --> 04:49.850
in terms of risk and return.

04:49.850 --> 04:57.530
We have to calculate Sharpe ratio and uh therefore we have to define the risk free return of the risk

04:57.530 --> 05:04.910
free asset and we use the five year US Treasury note that was issued at the end of 213 and the coupon

05:04.940 --> 05:07.430
rate was one point seven percent.

05:08.090 --> 05:13.540
And by definition the risk free risk of the risk free asset is a zero.

05:13.550 --> 05:21.370
So we say if risk free return and risk free risk here in a list and we are still our summary data frame

05:21.380 --> 05:27.710
with our constituents and then we can calculate the sharp ratio of our six constituents by adding an

05:27.770 --> 05:33.830
additional column sharp and we can actually calculate the sharp racial bias of selecting the risk we

05:33.830 --> 05:42.570
return from the return of the constituents divided by the risk and let's have a look here.

05:42.600 --> 05:46.160
The question here is which stocks showed the highest chop ratio.

05:46.620 --> 05:54.340
And obviously at this McDonald's and the same we can also do for our Random portfolios so we calculated

05:54.370 --> 06:04.110
the SAP ratio and we add another column sharp to our portfolio summary data frame A.B. f the last five

06:04.110 --> 06:11.310
random portfolios and the sharp ratio and the question is also what is the maximum Sharpe ratio and

06:11.460 --> 06:19.320
we can determine this with the described method and we can see that the maximum value in the column

06:19.320 --> 06:20.790
Sharp is the one.

06:20.790 --> 06:27.750
So this is the highest Sharpe Ratio and again we can plot our visualize the random portfolios and the

06:27.750 --> 06:34.050
constituents and we can actually create a third I mentioned the sharp Ray Shaw and the color will indicate

06:34.080 --> 06:38.130
whether the portfolio has a high or low Sharpe ratio.

06:38.130 --> 06:44.070
And it is always a bit tricky to select the appropriate values for women and we mix.

06:44.130 --> 06:48.650
But you can see here in the shop ratio column the highest Sharpe Ratio is 1.

06:48.780 --> 06:55.350
So we could pass the one to We Max and the minimum value here is so point 3 4.

06:55.350 --> 07:02.670
So we could use here maybe 0 point 5 for movement but this they are actually a trial and error process

07:02.700 --> 07:05.220
to get the most meaningful plot.

07:05.280 --> 07:07.100
So let's try this out here.

07:11.360 --> 07:18.020
And here we can see that here we have the portfolios with the highest chop ratios and being a dark red

07:18.050 --> 07:26.430
actually here the next question we have to get the ROE label off the max Sharpe Ratio portfolio and

07:26.580 --> 07:33.300
we can do this year with the idea X Max method on the shop column and by doing so we're getting the

07:33.360 --> 07:39.500
ROE label of the max operational portfolio and this is four thousand five hundred fifty five.

07:39.720 --> 07:46.330
And then we can also select that particular portfolio by passing five thousand five hundred fifty five

07:46.620 --> 07:49.090
to the local operator here.

07:49.680 --> 07:56.850
And here we have the return of 16 percent and the risk of 14 percent and the sharp ratio of 1 and then

07:56.850 --> 08:03.930
we can also get the rates of the constituents and the max Sharpe Ratio portfolio by passing here their

08:03.930 --> 08:06.890
own label which is also here the ROE index position.

08:06.990 --> 08:16.360
To the weights matrix are weights array and these are actually the weights and to make this more explicit.

08:16.410 --> 08:22.150
We can also add uh the stock takers and created a panel series.

08:22.440 --> 08:29.680
So we are 5 percent for American Express 16 for Facebook and for example 45 for McDonald's.

08:29.700 --> 08:34.890
And the question is Where do you think that the Sox American Express Procter and Gamble and Wal-Mart

08:35.010 --> 08:38.060
have any weights or weights higher than zero.

08:38.070 --> 08:45.030
And the real Max sharp Rachel portfolio and either we can you see uh optimization algorithms or we can

08:45.030 --> 08:51.480
simply increase the number of random portfolios and uh let's do this here.

08:51.690 --> 09:05.580
So we could increase to 50000 thousand and rerun the says here.

09:06.070 --> 09:10.380
So here we increase the number of portfolios to 50000

09:15.510 --> 09:22.920
so I free run our coding SATs and our max Sharpe Ratio portfolio has now a return of 18 percent and

09:22.920 --> 09:30.190
a risk of 15 percent and a sharp ratio of 4 one point 0 4 and let's have a look at the weights.

09:30.300 --> 09:36.930
So obviously we have zero weight for American Express for Procter and Gamble and from Walmart and we

09:36.930 --> 09:43.400
have kind of an concentrated position in the McDonald's stock and that's also if I look at the weights

09:43.400 --> 09:49.300
of the max Sharpe ratio that can be derived with optimization algorithms.

09:49.320 --> 09:55.170
So here are the weights and let's create a panel series and let's compare here.

09:55.230 --> 10:03.180
So indeed we have zero weight in those uh three stocks and we have even a higher concentration in McDonald's.

10:03.210 --> 10:05.550
So 62 percent.

10:05.550 --> 10:12.870
And even if this was the very best portfolio in the past it's uh not really optimal because it's pretty

10:12.870 --> 10:19.860
much concentrated in the McDonald's stock so nobody would invest in this portfolio here in a forward

10:19.890 --> 10:25.890
looking case and for the time being we are not finished yet with exercise 15 and I hope to see you also

10:25.890 --> 10:27.120
in the next exercise by.
